<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="query" size="mini">
        <el-form-item label="ID">
          <el-input v-model.trim="query.id" clearable placeholder="请输入ID"/>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model.trim="query.content" clearable placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="query.status" clearable style="width: 120px">
            <el-option label="未读" :value="0"></el-option>
            <el-option label="已读" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发信人ID">
          <el-input v-model.trim="query.fromId" clearable placeholder="请输入发信人ID"/>
        </el-form-item>
        <el-form-item label="收信人ID">
          <el-input v-model.trim="query.toId" clearable placeholder="请输入收信人ID"/>
        </el-form-item>
        <el-form-item label="会话ID">
          <el-input v-model.trim="query.conversationId" clearable placeholder="请输入会话ID"/>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            clearable
            v-model="query.begin"
            placeholder="开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="-">
          <el-date-picker
            clearable
            v-model="query.end"
            placeholder="结束时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-permission="'message:search'" icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          <el-button v-permission="'message:search'" icon="el-icon-refresh" @click="reload">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格显示列表数据 -->
    <el-table
      :data="list"
      stripe
      border
      height="650"
    >
      <el-table-column label="序号" width="70" fixed align="center">
        <template slot-scope="scope">
          <!--  page.current 当前页码；page.size 每页记录数 -->
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="200" align="center"/>
      <el-table-column prop="content" label="内容" width="500"/>
      <!-- 状态：( 0 false 未读；1 true 已读 ) -->
      <el-table-column align="center" prop="status" label="状态" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="">未读</el-tag>
          <el-tag v-if="scope.row.status == 1" type="info">已读</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updateTime" label="更新时间" width="200px"/>
      <el-table-column align="center" prop="createTime" label="创建时间" width="200px"/>
      <el-table-column prop="fromId" label="发信人ID" width="200" align="center"/>
      <el-table-column prop="toId" label="收信人ID" align="center" width="200"/>
      <el-table-column prop="conversationId" label="会话ID" align="center" width="300"/>
      <el-table-column align="center" label="操作" fixed="right">
        <template slot-scope="scope">
          <!-- 5、根据ID删除留言 v-permission="'article:delete'" -->
          <el-button v-permission="'message:delete'" type="danger" @click="handleDelete(scope.row.id)" size="mini">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      align="center"
      background
      :current-page="current"
      :page-sizes="[10, 20,30,50,100]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import api from '@/api/message'

export default {
  name: 'Message',
  data() {
    return {
      list: [],
      current: 1,
      size: 10,
      total: 0,
      query: {}
    }
  },
  created() {
    // 文章数据列表
    this.fetchData()
  },
  methods: {
    // 获取条件分页列表数据
    async fetchData() {
      const { data } = await api.getList(this.current, this.size, this.query)
      this.list = data.records
      this.total = Number(data.total)
    },
    // 切换每页显示多少条记录
    handleSizeChange(size) {
      this.size = size
      this.fetchData()
    },
    // 切换当前页码
    handleCurrentChange(current) {
      this.current = current
      this.fetchData()
    },
    // 条件查询
    queryData() {
      this.current = 1
      this.fetchData()
    },
    // 重置表单
    reload() {
      this.query = {}
      this.fetchData()
    },
    // 根据ID删除
    handleDelete(id) {
      this.$confirm('确定要删除这么记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.deleteById(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success(response.message)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped>
.el-pagination {
  margin: 20px 0 50px;
}

</style>
